package com.ydd.app.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.omg.CORBA._IDLTypeStub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ydd.app.bean.common.NewsDetailBean;
import com.ydd.app.bean.common.NewsTypeBean;
import com.ydd.app.bean.input.CreateNewsRequest;
import com.ydd.app.bean.input.DeleteNewsRequest;
import com.ydd.app.bean.input.QueryNewestNewsRequest;
import com.ydd.app.bean.input.QueryNewsDetailRequest;
import com.ydd.app.bean.input.QueryNewsInWebRequest;
import com.ydd.app.bean.input.QueryNewsRequest;
import com.ydd.app.bean.input.QueryNewsTypesRequest;
import com.ydd.app.bean.input.RewiewNewsRequest;
import com.ydd.app.bean.input.UpdateNewsRequest;
import com.ydd.app.bean.output.CreateNewsResponse;
import com.ydd.app.bean.output.DeleteNewsResponse;
import com.ydd.app.bean.output.QueryNewestNewsResponse;
import com.ydd.app.bean.output.QueryNewsDetailResponse;
import com.ydd.app.bean.output.QueryNewsInWebResponse;
import com.ydd.app.bean.output.QueryNewsResponse;
import com.ydd.app.bean.output.QueryNewsTypesResponse;
import com.ydd.app.bean.output.RewiewNewsResponse;
import com.ydd.app.bean.output.UpdateNewsResponse;
import com.ydd.app.manager.NewsMng;
import com.ydd.common.util.CommonUtil;

@Controller
public class NewsAct extends CommonAct{
	private static final Logger  log = Logger.getLogger(NewsAct.class);

	/**
	 * =============
	 * 新闻详细
	 * =============
	 */
	@RequestMapping(value = "/queryNewsDetail.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryNewsDetail(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryNewsDetailResponse ret = new QueryNewsDetailResponse();		
		try{
			// 参数获取
			QueryNewsDetailRequest input = new QueryNewsDetailRequest();	
			getParameterBean(request, input);

			final NewsDetailBean newsDetail = _newsMng.queryNewsDetail(input.getNewsId());
			if( null != newsDetail ) {
				ret.setNewsDetail(newsDetail);
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("未查到该资料");
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 创建新闻
	 * =============
	 */
	@RequestMapping(value = "/createNews.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String createNews(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		CreateNewsResponse ret = new CreateNewsResponse();		
		try{
			// 参数获取
			CreateNewsRequest input = new CreateNewsRequest();	
			getParameterBean(request, input);

//			final NewsDetailBean newsDetail =
					final boolean success = _newsMng.createNews(input.getNewsDetail(), input.getTitle(), input.getNewsTypeId(), input.getUserId(),
					input.getNewsDescribe(),
					input.getPictureUrl());
			if( success ) {
//				ret.setNewsDetail(newsDetail);
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("创建失败");
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 删除新闻
	 * =============
	 */
	@RequestMapping(value = "/deleteNews.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String deleteNews(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		DeleteNewsResponse ret = new DeleteNewsResponse();		
		try{
			// 参数获取
			DeleteNewsRequest input = new DeleteNewsRequest();	
			getParameterBean(request, input);

			final boolean deleteSuccess = _newsMng.deleteNews(input.getNewsId());
			if( deleteSuccess ) {
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("删除失败");
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 查询新闻列表
	 * =============
	 */
	@RequestMapping(value = "/queryNewsInBackstage.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryNewsInBackstage(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryNewsResponse ret = new QueryNewsResponse();		
		try{
			// 参数获取
			QueryNewsRequest input = new QueryNewsRequest();	
			getParameterBean(request, input);

			final NewsDetailBean[] news = _newsMng.queryNewsInBackstage(input.getPage(), input.getPageCount(), input.getNewsTypeId(),input.getNewsStatusId());
			if( null != news ) {
				ret.setNews(news);
				ret.setCode(STATIC_RET_OK);
				ret.setRowCount(_newsMng.queryNewsCountInBackstage(input.getNewsTypeId(),input.getNewsStatusId()));
			}
			else {
				ret.setNews(new NewsDetailBean[0]);
				ret.setCode(STATIC_RET_OK);
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 审核新闻
	 * =============
	 */
	@RequestMapping(value = "/rewiewNews.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String rewiewNews(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		RewiewNewsResponse ret = new RewiewNewsResponse();		
		try{
			// 参数获取
			RewiewNewsRequest input = new RewiewNewsRequest();	
			getParameterBean(request, input);

			final NewsDetailBean newsDetail = _newsMng.reviewNews(input.getNewsId(),input.getStatusId(),input.getReson(),input.getUserId());
			if( null != newsDetail ) {
				ret.setNewsDetail(newsDetail);
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("审核失败");
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 更新新闻
	 * =============
	 */
	@RequestMapping(value = "/updateNews.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String updateNews(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		UpdateNewsResponse ret = new UpdateNewsResponse();		
		try{
			// 参数获取
			UpdateNewsRequest input = new UpdateNewsRequest();	
			getParameterBean(request, input);

			final NewsDetailBean newsDetail = _newsMng.updateNews(input.getNewsId(), input.getNewsDetail(), input.getTitle(), input.getNewsTypeId(),
					input.getNewsDescribe(),
					input.getPictureUrl());
			if( null != newsDetail ) {
				ret.setNewsDetail(newsDetail);
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setCode(STATIC_RET_ERROR_2000);
				ret.setMessage("更新失败");
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 获取最新新闻
	 * =============
	 */
	@RequestMapping(value = "/queryNewestNews.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryNewestNews(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryNewestNewsResponse ret = new QueryNewestNewsResponse();		
		try{
			// 参数获取
			QueryNewestNewsRequest input = new QueryNewestNewsRequest();	
			getParameterBean(request, input);

			final NewsTypeBean[] news = _newsMng.queryNewestNews(Integer.parseInt(msg.getValue("newestNewsCount")));
			if( null != news ) {
				ret.setNewsTypes(news);
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setNewsTypes(new NewsTypeBean[0]);
				ret.setCode(STATIC_RET_OK);
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 网页端获取新闻列表
	 * =============
	 */
	@RequestMapping(value = "/queryNewsInWeb.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryNewsInWeb(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryNewsInWebResponse ret = new QueryNewsInWebResponse();		
		try{
			// 参数获取
			QueryNewsInWebRequest input = new QueryNewsInWebRequest();	
			getParameterBean(request, input);
			final String pageCount = msg.getValue("newsCountInWeb");
			if ( CommonUtil.isNotEmpty(pageCount)) {
				input.setPageCount(Integer.parseInt(pageCount));
			}
			final NewsDetailBean[] news = _newsMng.queryNewsInWeb(input.getPage(), input.getPageCount()
					, input.getNewsTypeId(), input.getKeyWord());
			if( null != news ) {
				ret.setNews(news);
				ret.setCode(STATIC_RET_OK);
				ret.setRowCount(_newsMng.queryNewsCountInWeb(input.getNewsTypeId()));
			}
			else {
				ret.setNews(new NewsDetailBean[0]);
				ret.setCode(STATIC_RET_OK);
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	/**
	 * =============
	 * 查询新闻分类列表
	 * =============
	 */
	@RequestMapping(value = "/queryNewsTypes.do", method = {RequestMethod.GET,RequestMethod.POST})
	public String queryNewsTypes(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + BEGIN_METHOD);
		// 接口返回配置
		String callback = request.getParameter(CALLBACK);
		QueryNewsTypesResponse ret = new QueryNewsTypesResponse();		
		try{
			// 参数获取
			QueryNewsTypesRequest input = new QueryNewsTypesRequest();	
			getParameterBean(request, input);

			final NewsTypeBean[] newsTypes = _newsMng.queryNewsTypes();
			if( null != newsTypes ) {
				ret.setNewsTypes(newsTypes);
				ret.setCode(STATIC_RET_OK);
			}
			else {
				ret.setNewsTypes(new NewsTypeBean[0]);
				ret.setCode(STATIC_RET_OK);
			}
			log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + END_METHOD);
		} catch(Exception e){
			// 接口执行失败
			ret.setCode(STATIC_RET_ERROR_1000);
			ret.setSuccess(false);
			ret.setMessage(msgUtils.getValue("系统异常"));
			
			log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":" + e.getMessage());
		}
		
		// BEAN转换为JSON数据格式
		CommBeanToJson(ret,response,callback);
		
		return RESULTAPP;
	}
	
	@Autowired
	private NewsMng _newsMng;

}
